<?php

namespace App\Trip\Orders\BookOrders\Train\Passengers;

use App\Trip\Orders\BookOrders\Params\AbstractOrderParam\AbstractOrderParam;
use App\Trip\Orders\BookOrders\Train\Order\AbstractOrder\AbstractBuildOrderInfo;
use App\Trip\Orders\BookOrders\Train\Passengers\AbstractOrderPassenger\AbstractOrderPassengers;
use App\Trip\Orders\BookOrders\Train\TrainStationTicket;
use App\Trip\Orders\TripOrderState;
use Exception;
use GuzzleHttp\Exception\GuzzleException;

class BookOrderPassengers extends AbstractOrderPassengers
{
    private array                  $passengers;
    private TrainStationTicket     $ticket;
    private AbstractOrderParam     $order_param;
    private AbstractBuildOrderInfo $orderInfo;

    /**
     * @throws Exception
     */
    public function __construct(AbstractBuildOrderInfo $orderInfo)
    {
        $this->orderInfo   = $orderInfo;
        $this->passengers  = $this->orderInfo->getPassengerParam()->getParamPassengers();
        $this->order_param = $this->orderInfo->getOrderParam();
        $this->ticket      = new TrainStationTicket($this->order_param->getTripParam());
    }

    /**
     * @throws GuzzleException
     * @throws Exception
     */
    public function buildPassengers(): array
    {
        $current_seat = $this->ticket->getSeat($this->order_param->getSeatName());
        $passengers   = $this->passengers;
        foreach ($passengers as $pk => $passenger) {
            $passenger_type                 = $passenger['passenger_type'];
            $passengers                     = $this->getPassengersCommonData($this->ticket->TrainInfo(), $current_seat, $passengers, $pk, $passenger_type);
            $passengers[$pk]['order_state'] = TripOrderState::ORDER_STATE_WAITING_PAY;
        }
        return $passengers;
    }


}